#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <sys/un.h>

#define NIT_SHORT_NAMES
#include <nit/socket.h>

#include "../tp.h"
#include "../tr.h"
#include "../io.h"

char msg[] = "this is a really really long message, so listen to "
	"the whole thing for no reason, even though it is really long";

void
interrupt(Nit_joint *in, Nit_joint *out)
{
        T3_trad trad;
	T3_tpre re = { .done = 0, .amt = 0 };
	T3_tpad ad = {
		.spc = 3,
		.chunk = 1,
	        .bytes = msg,
		.size = sizeof(msg) - 1
	};

	t3_trad_init(&trad, 0);

	while (!re.done) {
		t3_tp(in, out, &ad, &re);

		if (!re.done) {
			t3_tr(in, &trad);
			fprintf(stderr, "Hey! Stop interrupting!\n");
			ad.size -= re.amt;
			ad.bytes += re.amt;
		}
	}
}

int
main(int argc, char *argv[])
{
	Nit_joint *jnt_out = t3_io_single("t3/out");
	Nit_joint *jnt_in = t3_io_single("t3/in");

	t3_tr_rate_change(jnt_in, 0.0001);
	interrupt(jnt_in, jnt_out);

	joint_free(jnt_out);
	joint_free(jnt_in);
}
